package descentFly.app.managePanel.chat
{
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	public class df_turnScrollArea extends uif_ecomponent {
		public static var DEFAULT_MAXCOUNT: int = 0;
		private var _maxCount: int;
		public function df_turnScrollArea(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, 1, styleClass);
			_maxCount = DEFAULT_MAXCOUNT;
		}
		
		public function get maxCount(): int  {
			return _maxCount;
		}
		
		public function set maxCount(a_value : int): void  {
			_maxCount = a_value;
			while (_maxCount < numChildren) removeChildAt(0)
		}
		
		override public function addChild(child:DisplayObject): DisplayObject {
			if (_maxCount > 0)
				while (numChildren >= _maxCount) removeChildAt(0);
			
			child.y = height;
			if (height == 1) height = child.height; 
			else height += child.height;
			super.addChild(child);
			
			(child as uif_ecomponent).addEvent(uif_component.RESIZE, doItemResize);
			return child;
		}
		
		protected function doItemResize(e: Event): void {
			height = refreshItems();
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject {
			super.removeChild(child);
			height = refreshItems();
			return child;
		}

		override public function removeChildAt(index:int):DisplayObject {
			var child: DisplayObject = removeChildAt(index);
			height = refreshItems();
			return child;
		}
		
		protected function refreshItems(): int {
			var cy: int = 0;

			for (var i: int=0; i<numChildren; i++) {
				getChildAt(i).y = cy;
				cy += getChildAt(i).height;
			}
			return cy;
		}
	}
}